package com.sangeng.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.TagListDto;
import com.sangeng.domain.entity.Tag;
import com.sangeng.domain.vo.TagVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import com.sangeng.mapper.TagMapper;
import com.sangeng.service.TagService;
import com.sangeng.utils.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    private final TagMapper tagMapper;

    @Override
    public ResponseResult<List<TagVo>> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId, Tag::getName);
        List<Tag> tagList = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tagList, TagVo.class);
        return ResponseResult.okResult(tagVos);
    }

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        //name remark有值的时候才加上筛选条件
        wrapper.eq(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        wrapper.eq(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, wrapper);

        //封装数据返回
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(page.getRecords(), TagVo.class);
        PageVo pageVo = new PageVo(tagVos, page.getTotal());

//        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult<String> deleteTagById(List<Long> idList) {
        if (CollectionUtils.isNotEmpty(idList)) {
            boolean update = lambdaUpdate().set(Tag::getDelFlag, 1).in(Tag::getId, idList).update();
            return update ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.TAG_DELETE_ERROR);
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_ERROR);
    }

    @Override
    public ResponseResult<String> addTag(TagVo tagVo) {
        Tag tag = BeanCopyUtils.copyBean(tagVo, Tag.class);
        boolean save = save(tag);
        if (save) {
            return ResponseResult.okResult();
        } else {
            throw new SystemException(AppHttpCodeEnum.TAG_INSERT_FAILED);
        }
    }

    //修改标签 先获取原有信息 再执行更新操作
    @Override
    public ResponseResult<TagVo> getTagById(Long id) {
        Tag tag = tagMapper.selectById(id);
        //显示在页面的是VO
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);
        return ResponseResult.okResult(tagVo);
    }

    @Override
    public ResponseResult<String> updateTagInfo(TagVo tagVo) {
        Tag tag = BeanCopyUtils.copyBean(tagVo, Tag.class);
        boolean update = updateById(tag);
        if (update) {
            return ResponseResult.okResult();
        } else {
            throw new SystemException(AppHttpCodeEnum.TAG_UPDATE_ERROR);
        }
    }


}
